package com.moson.girl.handle;

import com.moson.girl.domain.Result;
import com.moson.girl.exception.GirlException;
import com.moson.girl.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理
 * @Author: MoXingjian
 * @Description:
 * @Date: Created in 20:02 2018/1/28
 * @Modified By:
 */
@ControllerAdvice
public class ExceptionHandle {

    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result handle(Exception e){
        //如果是自定义异常，获取状态码返回
        if (e instanceof GirlException) {
            GirlException girlException = (GirlException) e;
            return ResultUtil.error(girlException.getCode(),girlException.getMessage());
        } else {
            logger.error("error={}",e);
            return ResultUtil.error(-1, "未知错误");
        }
    }
}
